<?php
namespace app\company\controller;

use app\company\logic\Export;
class Order extends Base{

    //订单状态
    public static $order_static = [0=>'待付款', 1=>'待发货', 2=>'待收货', 3=>'已完成'];

    public function index(){
        //该企业下的用户
        $user_list = db('users')->where('com_id', $this->com_id)->column('id, name, mobile');
        $user_ids = [$this->user_id];

        $where = [
            'go.user_id'=>['in', $user_ids],
            'go.is_del'=>0
        ];
        input('mobile') && $where['u.mobile'] = ['like', input('mobile').'%'];

        if(input('export') == 1){
            $this->index_explode($where);
        }

        $list = db('goods_order')->alias('go')
            ->join('users u', 'u.id=go.user_id', 'LEFT')
            ->where($where)->order('go.id desc')->field('go.*,u.*,go.status status_a')->paginate('',false,page_param());
        $goods_order_ids = array_column($list->items(), 'id');
        $goods_list = db('goods_order_relation')->alias('gor')
            ->join('goods g', 'g.id=gor.goods_id', 'LEFT')
            ->where(['gor.goods_order_id'=>['in', $goods_order_ids]])
            ->column('gor.id, gor.goods_order_id, g.name');

        $this->assign('list', $list);
        $this->assign('user_list', $user_list);
        $this->assign('goods_list', $goods_list);
        return $this->fetch();
    }

    //产品订单编辑
    public function index_edit(){
        $id = I('id',0);
        $list = M('goods_order a')
            ->join('users c','a.user_id=c.id','left')
            ->field('a.*,c.account, c.com_id')
            ->where('a.id',$id)
            ->find();
        if($list['com_id'] != $this->com_id){
            $this->error('企业订单有误');
        }
        $goods_list = M('goods_order_relation a')
            ->join('goods b','b.id=a.goods_id','left')
            ->field('a.*,b.name,b.price,b.images')
            ->where('a.goods_order_id',$list['id'])
            ->select();
        $list['goods_list'] = $goods_list;
        $this->assign('info',$list);
        return $this->fetch();
    }
    //商品订单导出@stone
    public function index_explode($where){
        set_time_limit(0);
        $user_list = db('users')->where('com_id', $this->com_id)->column('id, name, mobile');
        $user_ids = array_column($user_list, 'id');
        $list = db('goods_order')->alias('go')
            ->join('users u', 'u.id=go.user_id', 'LEFT')
            ->where($where)->order('go.id desc')->select();
        $goods_order_ids = array_column($list, 'id');
        $goods_list = db('goods_order_relation')->alias('gor')
            ->join('goods g', 'g.id=gor.goods_id', 'LEFT')
            ->where(['gor.goods_order_id'=>['in', $goods_order_ids]])
            ->column('gor.id, gor.goods_order_id, g.name');
        $title = [
            '用户名',
            '电话',
            '订单号',
            '产品名',
            '支付金额',
            '创建时间',
            '订单状态',
        ];
        $val = [];
        foreach ($list as $key=>$value){
            $goods_name = '';
            foreach($goods_list as $k=>$vo){
                if($vo['goods_order_id'] == $value['id']){
                    $goods_name .= "{$vo['name']} ";
                }
            }
            $val[]=[
                $user_list[$value["user_id"]]['name']??'',
                $user_list[$value["user_id"]]['mobile']??'',
                $value["order_number"],
                $goods_name,
                $value['total_price'],
                date('Y-m-d H:i', $value['create_time']),
                self::$order_static[$value['status']]??''
            ];
        }
        $name = '产品订单';
        //导出excel
        Export::excel($title,$val,$name,20,['C']);
    }


    /**
     * 课程订单
     */
    public function course(){
        //该企业下的用户
        $user_list = db('users')->where('com_id', $this->com_id)->column('id, name, mobile');
        $user_ids = array_column($user_list, 'id');

        $where = [
            'co.user_id'=>['in', $user_ids],
            'co.is_del'=>0
        ];
        input('mobile') && $where['u.mobile'] = ['like', input('mobile').'%'];

        if(input('export') == 1){
            $this->course_explode($where);
        }

        $list = db('course_order')->alias('co')
            ->join('users u', 'u.id=co.user_id', 'LEFT')
            ->where($where)->order('co.id desc')
            ->field('co.id, co.order_number, co.user_id, co.course_id, co.people_sum, co.create_time, u.name, u.mobile')
            ->paginate('',false,page_param());
        $course_ids = array_column($list->items(), 'course_id');
        $course_list = db('course')->where(['id'=>['in', $course_ids]])->column('id, name, course_time');

        $this->assign('list', $list);
        $this->assign('course_list', $course_list);
        return $this->fetch();
    }

    //课程订单编辑
    public function course_edit(){
        $id = I('id',0);
        $info = M('course_order a')
            ->join('course b','a.course_id=b.id','left')
            ->join('users c','a.user_id=c.id','left')
            ->field('a.*,b.name goods_name,b.course_time,c.account,c.mobile, c.com_id')
            ->where('a.id',$id)
            ->find();
        if($info['com_id'] != $this->com_id){
            $this->error('企业订单有误');
        }
        $this->assign('info',$info);
        return $this->fetch();
    }

    public function course_explode($where){
        set_time_limit(0);

        $list = db('course_order')->alias('co')
            ->join('users u', 'u.id=co.user_id', 'LEFT')
            ->where($where)->order('co.id desc')
            ->field('co.id, co.order_number, co.user_id, co.course_id, co.people_sum, co.create_time, u.name, u.mobile')
            ->paginate('',false,page_param());
        $course_ids = array_column($list->items(), 'course_id');
        $course_list = db('course')->where(['id'=>['in', $course_ids]])->column('id, name, course_time');

        $strTable = '<table width="500" border="1">';
        $strTable .= '<tr>';
        $strTable .= '<td style="text-align:center;font-size:12px;width:*">订单号</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;width:*">用户名</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;width:120px;">联系电话</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;" width="*">课程名称</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;" width="*">购买人数</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;" width="*">开课时间</td>';
        $strTable .= '</tr>';
        if (!empty($list)) {
            foreach ($list as $key => $v) {
                $strTable .= '<tr>';
                $strTable .= '<td style="text-align:left;font-size:12px;">' . $v["order_number"] . '</td>';
                $strTable .= '<td style="text-align:left;font-size:12px;">' . $v["name"] . '</td>';
                $strTable .= '<td style="text-align:left;font-size:12px;">' . $v["mobile"] . '</td>';
                $strTable .= '<td style="text-align:left;font-size:12px;">' . $course_list[$v["course_id"]]['name'] . '</td>';
                $strTable .= '<td style="text-align:left;font-size:12px;">' . $v["people_sum"] . '</td>';
                $strTable .= '<td style="text-align:left;font-size:12px;">' . date('Y-m-d H:i', $course_list[$v["course_id"]]['course_time']). '</td>';
                $strTable .= '</tr>';
            }
        }
        $strTable .= '</table>';
        unset($list);
        $name = '课程订单';
        downloadExcel($strTable, $name);
        exit();
    }
}